 package com.bluexmicro.bluetooth

import android.bluetooth.BluetoothStatusCodes
import android.content.Context
import android.content.Intent
import android.util.Log
import org.json.JSONObject
import java.lang.Exception
import java.lang.ref.WeakReference

const val ACTION_LOG = "com.livsdesign.bluetooth.action.log"
const val LOG_CONTENT = "content_json"

object LogUtils {

    private var contextWeakRef = WeakReference<Context>(null)

    fun setup(context: Context) {
        contextWeakRef = WeakReference(context.applicationContext)
    }

    fun sendLog(params: Map<String, String>) {
        val content = try {
            JSONObject(params).toString()
        } catch (e: Exception) {
            e.printStackTrace()
            params.toString()
        }
        val intent = Intent(ACTION_LOG)
        intent.putExtra(LOG_CONTENT, content)
        contextWeakRef.get()?.sendBroadcast(intent)
    }

}

fun Int.bluetoothStatusCodesName(): String {
    return when (this) {
        BluetoothStatusCodes.SUCCESS -> "SUCCESS"
        BluetoothStatusCodes.ERROR_BLUETOOTH_NOT_ALLOWED -> "ERROR_BLUETOOTH_NOT_ALLOWED"
        BluetoothStatusCodes.ERROR_BLUETOOTH_NOT_ENABLED -> "ERROR_BLUETOOTH_NOT_ENABLED"
        BluetoothStatusCodes.ERROR_DEVICE_NOT_BONDED -> "ERROR_DEVICE_NOT_BONDED"
        BluetoothStatusCodes.ERROR_GATT_WRITE_NOT_ALLOWED -> "ERROR_GATT_WRITE_NOT_ALLOWED"
        BluetoothStatusCodes.ERROR_GATT_WRITE_REQUEST_BUSY -> "ERROR_GATT_WRITE_REQUEST_BUSY"
        BluetoothStatusCodes.ERROR_MISSING_BLUETOOTH_CONNECT_PERMISSION -> "ERROR_MISSING_BLUETOOTH_CONNECT_PERMISSION"
        BluetoothStatusCodes.ERROR_PROFILE_SERVICE_NOT_BOUND -> "ERROR_PROFILE_SERVICE_NOT_BOUND"
        BluetoothStatusCodes.FEATURE_NOT_SUPPORTED -> "FEATURE_NOT_SUPPORTED"
        BluetoothStatusCodes.FEATURE_SUPPORTED -> "FEATURE_SUPPORTED"
        else -> "ERROR_UNKNOWN"
    }
}


